package a66weding.com.jiehuntong.activity.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.LoginActivity;
import a66weding.com.jiehuntong.activity.ShopDetailsActivity;
import a66weding.com.jiehuntong.activity.home.hotel.hotelnew.HomeHotelActivity2;
import a66weding.com.jiehuntong.activity.home.hotel.shop.HomeProductdetailActivity;
import a66weding.com.jiehuntong.adapter.ShopAdapter;
import a66weding.com.jiehuntong.adapter.StoreMyListAdapter;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CaseBean;
import a66weding.com.jiehuntong.beans.CasesDetail;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HomeBotomListBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.StoreDetailGuess;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.common.utils.Tools;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.view.CircleImageView;
import a66weding.com.jiehuntong.view.ConfirmLoginDialog;
import a66weding.com.jiehuntong.widgets.MyListView;
import a66weding.com.jiehuntong.widgets.NoScrollGridView;
import a66weding.com.jiehuntong.widgets.dialog.MyRedheartDialog;
import a66weding.com.jiehuntong.widgets.scrollreflash.PullToLoadScroll;
import a66weding.com.jiehuntong.widgets.scrollreflash.PullToLoadScrollLissener;


/**
 * Created by admin on 2016/8/2.
 */
public class HomeStoreDetailsActivity extends BaseHomeActivity implements OnNetResultListener,PullToLoadScrollLissener , AdapterView.OnItemClickListener,View.OnClickListener{
    public static String STOREDETAILSACTIVITY = "StoreDetailsActivity";
    private CaseBean.DataBean  bean;
    private  int casesID;
    private PullToLoadScroll mSc;
    private  View homeView;
    private NoScrollGridView gridView;
    private boolean isDownRefresh = true;
    private MyListView lv;
    private TextView title;
    private CircleImageView  circularImage;
    private CircleImageView view_home_icon_below;
    private List<HomeBotomListBean.DataBean> homeBotomListBeanList;
    private  TextView view_home_jeishoa;
    private TextView view_home_fensi;
    private TextView view_home_jinru;
    private ShopAdapter adapter;
    private CircleImageView midle_circularImage;
    private String title1;
    private CasesDetail casebean;
    private StoreDetailGuess gussbean;
    private TextView mBackTv;
    private ImageView mAttentionIv;
    public static int getAttenRes=6;
    private boolean islogin;
    private int memBerId;
    private boolean isCollectionStare;
    private boolean isByResult;
    private  final int  ATTEN=2;
    private  final int  DIALOG=1;
    private MyRedheartDialog myDialog;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DIALOG:
                    if(myDialog!=null) {
                        myDialog.dismiss();
                    }
                    break;
                case ATTEN:
                    if(isAttention) {
                        mAttentionIv.setImageResource(R.mipmap.img_home_already_attention);
                    }else{
                        mAttentionIv.setImageResource(R.mipmap.attentiontext);
                    }
                    break;
            }
        }
    };
    private boolean isAttention;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_details);

        islogin = ((GlobalApplication) getApplication()).isLogin();
        if(islogin) {
            memBerId = DataWareHourse.getInstance().getmUser().getData().get(0).getI_Id();
        }
        findViewById(R.id.title_tv).setVisibility(View.INVISIBLE);
        Intent intent = getIntent();
        if(intent!=null) {
            title1 = intent.getStringExtra("title");
            casesID = intent.getIntExtra("casesID",0);
            isAttention = intent.getBooleanExtra("isAttention", false);
        }
        initView();
        getNetData();
    }
    private void initView(){
        homeBotomListBeanList = new ArrayList<>();
        mSc = (PullToLoadScroll) findViewById(R.id.scrollView_pl);
        homeView = View.inflate(this,R.layout.view_home,null);
        mSc.setContentView(homeView);
        mSc.setCall(this);
        gridView= (NoScrollGridView) homeView.findViewById(R.id.HomeView_Lv);
        gridView.setOnItemClickListener(this);
        adapter = new ShopAdapter(this,null);
        gridView.setAdapter(adapter);
        title = (TextView) homeView.findViewById(R.id.view_home_title);
        lv = (MyListView) findViewById(R.id.view_home_lv);
        circularImage = (CircleImageView) findViewById(R.id.view_home_icon);
        view_home_icon_below = (CircleImageView) findViewById(R.id.view_home_icon_below);
        view_home_jeishoa = (TextView) findViewById(R.id.view_home_jeishoa);
        view_home_fensi = (TextView) findViewById(R.id.view_home_fensi);
        view_home_jinru = (TextView) findViewById(R.id.view_home_jinru);
        view_home_jinru.setOnClickListener(this);
        midle_circularImage = (CircleImageView) findViewById(R.id.view_home_icon_below);
        mAttentionIv = (ImageView) homeView.findViewById(R.id.cate_attention);
        mBackTv = (TextView) findViewById(R.id.back_iamge);
        if(!TextUtils.isEmpty(title1)) {
            mBackTv.setText(title1);
        }
        if(isAttention) {
            mAttentionIv.setImageResource(R.mipmap.img_home_already_attention);
        }else{
            mAttentionIv.setImageResource(R.mipmap.attentiontext);
        }
        mAttentionIv.setOnClickListener(this);
    }

    private void initData(){
        x.image().bind(midle_circularImage,bean.getBusiness().getNvc_Logo(),ImageOptionsUtil.getOptions292_700());
        view_home_jeishoa.setText(bean.getBusiness().getNvc_StoreName());
        view_home_fensi.setText("已有"+bean.getBusiness().getBusinessMemberCount()+"个粉丝");
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Tools.dismissWaitDialog();
        Object data = sucessbean.getData();
        if(data instanceof CasesDetail){
            casebean= (CasesDetail) data;
            switch (casebean.getResCode()){
                case "0000":
                    String[] str = casebean.getData().getNvc_Content().split(",");
                    StoreMyListAdapter storeMyListAdapter = new StoreMyListAdapter(this,str);
                    lv.setAdapter(storeMyListAdapter);
                    title.setText(casebean.getData().getBusiness().getNvc_StoreName());
                    x.image().bind(circularImage,casebean.getData().getBusiness().getNvc_Logo(),ImageOptionsUtil.getOptions316_720());

                    break;
            }
        } else if (data instanceof  StoreDetailGuess){
            mSc.commplate();
            gussbean = (StoreDetailGuess) data;
            switch (gussbean.getResCode()){
                case "0000":
                    if (isDownRefresh){
                        adapter.clear();
                    }
                    adapter.append(gussbean.getData());
                    break;
                case "0003":
                    //数据为空;
                    mSc.commplate();
                    break;
            }
        } else if (data instanceof CommonResultBean){
            CommonResultBean commonData= (CommonResultBean) data;
            switch (commonData.getResCode()) {
                case "0000":
                    if(isCollectionStare) {
                        mAttentionIv.setImageResource(R.mipmap.img_home_already_attention);
                        myDialog = new MyRedheartDialog(this,"关注成功!");
                        myDialog.show();
                        mHandler.sendEmptyMessageDelayed(DIALOG,800);
                        if(casebean!=null) {
                            casebean.getData().getBusiness().setCollectionStare(true);
                        }
                        isAttention=true;
                    }else{
                        mAttentionIv.setImageResource(R.mipmap.attentiontext);
                        myDialog = new MyRedheartDialog(this,"成功取消关注!");
                        myDialog.show();
                        mHandler.sendEmptyMessageDelayed(DIALOG,800);
                        if(casebean!=null) {
                            casebean.getData().getBusiness().setCollectionStare(false);
                        }
                        isAttention=false;
                    }

                    break;
                case "0003":
                    break;
            }
        }
        if (gussbean!=null){
            changeData(gussbean.getData());
        }
    }
    @Override
    public void onError(ErrorBean errorBean) {
    }
    private void getNetData(){
        Tools.showDialog(this);
        //查询案例详细 GET WeddingMobileApi/get_CasesDetail?Token={Token}&casesID={casesID}
        String url = Url.GET_CASESDETAIL+"casesID="+casesID;
        HttpRequestBean bottomListRequestBean = new HttpRequestBean(RequestCode.GETREQUEST,url,this, CasesDetail.class,RequestCode.GET_CASESDETAIL);
        HttpHandleUtil.executeCommonGet(this,bottomListRequestBean);
    }
    private int pageIndex = 1;
    private void getData(int pageIndex){
        String url= RequestCode.get_CasesProductListLike;
        url += "&pageIndex="+pageIndex+ "&PCategory="+casesID;
        HttpRequestBean bottomListRequestBean = new HttpRequestBean(RequestCode.GETREQUEST,url,this, StoreDetailGuess.class,RequestCode.get_CasesProductListLike);
        HttpHandleUtil.executeCommonGet(this,bottomListRequestBean);
    }

    @Override
    public void RefrashCall() {
        isDownRefresh = true;
        pageIndex = 1;
        getNetData();
        getData(1);
    }

    @Override
    public void LoadCall() {
        isDownRefresh = false;
        ++pageIndex ;
        getData(pageIndex);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this,HomeProductdetailActivity.class);
        intent.putExtra("productId",homeBotomListBeanList.get(position).getI_Id());
        startActivity(intent);
    }

    public void back(View view){
        finish();
    }

    private void changeData(List<StoreDetailGuess.DataBean> dataBeen){
        if (isDownRefresh) {
            homeBotomListBeanList.clear();
        }
        HomeBotomListBean.DataBean.BusinessBean businessBean = new  HomeBotomListBean.DataBean.BusinessBean();
        businessBean.setNvc_StoreName(casebean.getData().getBusiness().getNvc_StoreName());
        businessBean.setNvc_Address(casebean.getData().getBusiness().getNvc_Address());
        for (int i = 0; i < dataBeen.size(); i++) {
            HomeBotomListBean.DataBean homeBean = new HomeBotomListBean.DataBean();
            homeBean.setI_Collections(dataBeen.get(i).getI_Collections());
            homeBean.setI_Category(dataBeen.get(i).getI_Category());
            homeBean.setDm_Price(dataBeen.get(i).getDm_Price());
            homeBean.setDm_ShopPrice(dataBeen.get(i).getDm_ShopPrice());
            homeBean.setNvc_PicMain(dataBeen.get(i).getNvc_PicMain());
            homeBean.setNvc_Name(dataBeen.get(i).getNvc_Name());
            homeBean.setBusiness(businessBean);
            homeBean.setI_Id(dataBeen.get(i).getI_Id());
            homeBotomListBeanList.add(homeBean);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.view_home_jinru:

                Intent intent = new Intent(this,ShopDetailsActivity.class);
                intent.putExtra("nvc_num",""+casebean.getData().getBusiness().getNvc_No());
                intent.putExtra("BusinessID",casebean.getData().getBusiness().getI_Id());
                startActivityForResult(intent,getAttenRes);

                break;
            case R.id.cate_attention:

                Intent intent2 = new Intent(this,ShopDetailsActivity.class);
                intent2.putExtra("nvc_num",""+casebean.getData().getBusiness().getNvc_No());
                intent2.putExtra("BusinessID",casebean.getData().getBusiness().getI_Id());
                startActivityForResult(intent2,getAttenRes);

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==getAttenRes) {
            if(data!=null) {
              isAttention=  data.getBooleanExtra("isAtten",false);
                mHandler.sendEmptyMessage(ATTEN);
            }
        }
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK ) {
            setResult(HomeHotelActivity2.getAttentionRes);
            finish();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
    public  void showLoginDialog(Context context, String title, String confirm, String cancel) {
        final ConfirmLoginDialog confirmDialog = new ConfirmLoginDialog(context, title, confirm, cancel,R.layout.pop_tologin);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmLoginDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                Intent intent = new Intent( HomeStoreDetailsActivity.this, LoginActivity.class);
                HomeStoreDetailsActivity.this.startActivity(intent);
                confirmDialog.dismiss();
            }
            @Override
            public void doCancel() {
                confirmDialog.dismiss();
            }
        });
    }
}
